package com.ruoyi.web.controller.tool;

import cn.hutool.http.HttpUtil;
import com.ruoyi.common.utils.http.HttpUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.ruoyi.common.core.controller.BaseController;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * swagger 接口
 *
 * @author ruoyi
 */
@Controller
@RequestMapping("/tool/swagger")
public class SwaggerController extends BaseController {
    @PreAuthorize("@ss.hasPermi('tool:swagger:view')")
    @GetMapping()
    public String index() {
        return redirect("/swagger-ui.html");
    }

    public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream(new File("D:\\usr\\R-C-2.jpg"));
            byte[] bytes = IOUtils.toByteArray(fis);

            // 对字节数组进行Base64编码，得到Base64编码的字符串
            BASE64Encoder encoder = new BASE64Encoder();
            String base64Str = encoder.encode(bytes);

            System.out.println(base64Str);

            FileUtils.write(new File("D:/usr/base64.txt"),base64Str);
            //将base64数据转为本地图片
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] bfile = decoder.decodeBuffer(base64Str);
            FileUtils.writeByteArrayToFile(new File("D:/tools/nginx-1.18.0/nginx-1.18.0/html/images/"+System.currentTimeMillis()+".jpg"),bfile);


            String body="{\n" +
                    "  \"action\": \"upload_image\",\n" +
                    "  \"Format\": \"JPG\",\n" +
                    "  \"PatientName\": \"LIU BAO XIA\",\n" +
                    "  \"PatientID\": \"112145\",\n" +
                    "  \"StudyDate\": \"20230327\",\n" +
                    "  \"StudyInsta\": \"1.2.826.0.1.3680043.6.13460.9766.20210624080140.632.144\",\n" +
                    "  \"Position\": \"左侧/右侧\",\n" +
                    "  \"picFile\": \"394279_4CD2160.jpg\",\n" +
                    "  \"picStr\": \""+base64Str+"\",\n" +
                    "  \"dcmFile\": \"394279_4CD2160.dcm\",\n" +
                    "  \"dcmStr\": \"394279_4CD2160.dcm\",\n" +
                    "  \"ImaDesc\": \"左腋下肿瘤\"\n" +
                    "}";
            HttpUtil.post("http://115.158.64.111:8080/api/tspet/capture",body);

            System.out.println("已发送");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
